<%- include("header",{type:'all'}) %>
<div class="container">
    <ul class="posts">
        <% posts.forEach(function(res){ %>
        <li>
            <div class="author">
                <span title="<%= res.name %>"><a
                            href="/posts?author=<%= res.name %> ">author: <%= res.name %></a></span>
                <span>评论数：<%= res.comments %></span>
                <span>浏览量：<%= res.pv %></span>
            </div>
            <div class="comment_pv">
                <span><%= res.moment %></span>
            </div>
            <a href="/posts/<%= res.id %>">
                <div class="title">
                    <img class="userAvator" src="images/<%= res.avator %>.png">
                    <%= res.title %>
                </div>
                <div class="content markdown">
                    <%- res.content %>
                </div>
            </a>
        </li>
        <% }) %>
    </ul>
    <div style="margin-top: 30px" class="pagination" id="page"></div>
</div>
<script src="http://www.wclimb.site/pagination/pagination.js"></script>
<script>
    pagination({
        selector: '#page',
        totalPage: <%= postsPageLength %>,
        currentPage: 1,
        prev: '上一页',
        next: '下一页',
        first: true,
        last: true,
        showTotalPage: true,
        count: 2//当前页前后显示的数量
    }, function (val) {
        // 当前页
        $.ajax({
            url: "posts/page",
            type: 'POST',
            data: {
                page: val
            },
            cache: false,
            success: function (msg) {
                console.log(msg)
                if (msg != 'error') {
                    $('.posts').html(' ')
                    $.each(msg, function (i, val) {
                        //console.log(val.content)
                        $('.posts').append(
                            '<li>' +
                            '<div class=\"author\">' +
                            '<span title=\"' + val.name + '\"><a href=\"/posts?author=' + val.name + ' \">author: ' + val.name + '</a></span>' +
                            '<span>评论数：' + val.comments + '</span>' +
                            '<span>浏览量：' + val.pv + '</span>' +
                            '</div>' +
                            '<div class=\"comment_pv\">' +
                            '<span>' + val.moment + '</span>' +
                            '</div>' +
                            '<a href=\"/posts/' + val.id + '\">' +
                            '<div class=\"title\">' +
                            '<img class="userAvator" src="images/' + val.avator + '.png">' +
                            val.title +
                            '</div>' +
                            '<div class=\"content\">' +
                            val.content +
                            '</div>' +
                            '</a>' +
                            '</li>'
                        )
                    })
                } else {
                    alert('分页不存在')
                }
            }
        })
    })

    // 单篇文章页
    router.get('/posts/:postId', async (ctx, next) => {
        let comment_res,
            res,
            pageOne,
            res_pv;
        await userModel.findDataById(ctx.params.postId)
            .then(result => {
                //console.log(result )
                res = result
                res_pv = parseInt(result[0]['pv'])
                res_pv += 1
                // console.log(res_pv)
            })
        await userModel.updatePostPv([res_pv, ctx.params.postId])
        await userModel.findCommentByPage(1, ctx.params.postId)
            .then(result => {
                pageOne = result
                //console.log('comment', comment_res)
            })
        await userModel.findCommentById(ctx.params.postId)
            .then(result => {
                comment_res = result
                //console.log('comment', comment_res)
            })
        await ctx.render('sPost', {
            session: ctx.session,
            posts: res[0],
            commentLenght: comment_res.length,
            commentPageLenght: Math.ceil(comment_res.length / 10),
            pageOne: pageOne
        })

    })
</script>
<% include footer %>